﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using RooHui.Infrastructure.Entities.EFContext;

namespace RooHui.Infrastructure.Entities.Repository
{
    [Table("P_AmountOperation")]
    public class P_AmountOperation : IEntity<P_AmountOperation>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string SequenceNO { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string UserName { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(50)]
        public string Bank { get; set; }
        public decimal Amount { get; set; }
        public int OperationType { get; set; }
        [Column(TypeName = "varchar")]
        public string SplitAmount { get; set; }
        public DateTime CreateTime { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(20)]
        public string ResultCode { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string ResultNote { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string PaySequenceNo { get; set; }
        public DateTime PayBackTime { get; set; }
        [Column(TypeName = "bit")]
        public bool IsSuccess { get; set; }
        public int Status { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(20)]
        public string IpAddress { get; set; }
        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string Summary { get; set; }
        [Column(TypeName="varchar")]
        [StringLength(32)]
        public string RefId { get; set; }

        public override P_AmountOperation Find(string key)
        {
            int Id = Int32.Parse(key);
            return Find(c => c.ID == Id);
        }

        public override P_AmountOperation Find(System.Linq.Expressions.Expression<Func<P_AmountOperation, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.AmountOperation.FirstOrDefault(where);
                if (item != null)
                {
                    this.Amount = item.Amount;
                    this.Bank = item.Bank;
                    this.CreateTime = item.CreateTime;
                    this.ID = item.ID;
                    this.IpAddress = item.IpAddress;
                    this.IsSuccess = item.IsSuccess;
                    this.OperationType = item.OperationType;
                    this.PayBackTime = item.PayBackTime;
                    this.PaySequenceNo = item.PaySequenceNo;
                    this.ResultCode = item.ResultCode;
                    this.ResultNote = item.ResultNote;
                    this.SequenceNO = item.SequenceNO;
                    this.SplitAmount = item.SplitAmount;
                    this.Status = item.Status;
                    this.UserName = item.UserName;
                    this.Summary = item.Summary;
                    this.RefId = item.RefId;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<P_AmountOperation, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.AmountOperation.Any(where);
            }
        }
    }
}